using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace TenantManagementService.Tenants
{
    /// <summary>
    /// 租户配置实体
    /// </summary>
    public class TenantConfiguration : CreationAuditedAggregateRoot<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; protected set; }

        /// <summary>
        /// 配置键
        /// </summary>
        public string Key { get; protected set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; protected set; } = string.Empty;

        /// <summary>
        /// 配置组
        /// </summary>
        public string? Group { get; protected set; }

        /// <summary>
        /// 配置描述
        /// </summary>
        public string? Description { get; protected set; }

        /// <summary>
        /// 是否加密存储
        /// </summary>
        public bool IsEncrypted { get; protected set; } = false;

        /// <summary>
        /// 是否只读
        /// </summary>
        public bool IsReadOnly { get; protected set; } = false;

        /// <summary>
        /// 配置类型
        /// </summary>
        public ConfigurationType Type { get; protected set; } = ConfigurationType.String;

        /// <summary>
        /// 默认值
        /// </summary>
        public string? DefaultValue { get; protected set; }

        /// <summary>
        /// 验证规则（JSON格式）
        /// </summary>
        public string? ValidationRules { get; protected set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; protected set; }

        /// <summary>
        /// 最后修改者
        /// </summary>
        public string? LastModifierId { get; protected set; }

        protected TenantConfiguration()
        {
        }

        public TenantConfiguration(
            Guid id,
            Guid? tenantId,
            string key,
            string value,
            ConfigurationType type = ConfigurationType.String
        ) : base(id)
        {
            TenantId = tenantId;
            Key = Check.NotNullOrWhiteSpace(key, nameof(key));
            Value = value ?? string.Empty;
            Type = type;
        }

        /// <summary>
        /// 更新配置值
        /// </summary>
        public virtual TenantConfiguration UpdateValue(string value)
        {
            Value = value ?? string.Empty;
            LastModificationTime = DateTime.Now;
            return this;
        }

        /// <summary>
        /// 设置配置组
        /// </summary>
        public virtual TenantConfiguration SetGroup(string? group)
        {
            Group = group;
            return this;
        }

        /// <summary>
        /// 设置描述
        /// </summary>
        public virtual TenantConfiguration SetDescription(string? description)
        {
            Description = description;
            return this;
        }

        /// <summary>
        /// 设置是否加密
        /// </summary>
        public virtual TenantConfiguration SetEncryption(bool isEncrypted)
        {
            IsEncrypted = isEncrypted;
            return this;
        }

        /// <summary>
        /// 设置是否只读
        /// </summary>
        public virtual TenantConfiguration SetReadOnly(bool isReadOnly)
        {
            IsReadOnly = isReadOnly;
            return this;
        }

        /// <summary>
        /// 设置配置类型
        /// </summary>
        public virtual TenantConfiguration SetType(ConfigurationType type)
        {
            Type = type;
            return this;
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        public virtual TenantConfiguration SetDefaultValue(string? defaultValue)
        {
            DefaultValue = defaultValue;
            return this;
        }

        /// <summary>
        /// 设置验证规则
        /// </summary>
        public virtual TenantConfiguration SetValidationRules(string? validationRules)
        {
            ValidationRules = validationRules;
            return this;
        }
    }

    /// <summary>
    /// 配置类型枚举
    /// </summary>
    public enum ConfigurationType
    {
        /// <summary>
        /// 字符串
        /// </summary>
        String = 0,

        /// <summary>
        /// 整数
        /// </summary>
        Integer = 1,

        /// <summary>
        /// 浮点数
        /// </summary>
        Float = 2,

        /// <summary>
        /// 布尔值
        /// </summary>
        Boolean = 3,

        /// <summary>
        /// 日期时间
        /// </summary>
        DateTime = 4,

        /// <summary>
        /// JSON对象
        /// </summary>
        JsonObject = 5,

        /// <summary>
        /// JSON数组
        /// </summary>
        JsonArray = 6,

        /// <summary>
        /// 加密字符串
        /// </summary>
        EncryptedString = 7,

        /// <summary>
        /// 文件路径
        /// </summary>
        FilePath = 8,

        /// <summary>
        /// URL
        /// </summary>
        Url = 9,

        /// <summary>
        /// 邮箱地址
        /// </summary>
        Email = 10,

        /// <summary>
        /// 颜色值
        /// </summary>
        Color = 11
    }
}